package cn.hh.ymcc.mq.consumer;

import cn.hh.ymcc.domain.MediaFile;
import cn.hh.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
@Component // 交给容器管理
@RocketMQMessageListener(consumerGroup = "medis-consumer-group",topic = "media-topic",selectorExpression = "media-tags")
public class MessageFileConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService; // 注入


    @Override
    public void onMessage(MessageExt messageExt) {
        // 调用messageExt.getBody()获取到消息体, 将获取到的消息体转成字符串类型对象jsonStr
       String jsonStr =  new String(messageExt.getBody(), StandardCharsets.UTF_8);

        // 将字符串类型的消息体对象jsonStr,强转为MediaFile
        MediaFile mediaFile = JSONObject.parseObject(jsonStr, MediaFile.class);

        // 使用mediaFileService调用方法传入mediaFile消息体对象, handleFile2m3u8:处理文件对象,将文件转成m3u8
        mediaFileService.handleFile2m3u8(mediaFile);

    }

}
